package com.ruoyi.crm.service.impl;

import com.ruoyi.crm.domain.CrmUser;
import com.ruoyi.crm.mapper.UserMapper;
import com.ruoyi.crm.service.CrmUserService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import javax.annotation.Resource;

/**
 * 用户信息表(CrmUser)表服务实现类
 *
 * @author makejava
 * @since 2022-06-23 16:25:43
 */
@Service
public class CrmUserServiceImpl implements CrmUserService {
	@Resource
	private UserMapper userMapper;

	/**
	 * 通过ID查询单条数据
	 *
	 * @param userId 主键
	 * @return 实例对象
	 */
	@Override
	public CrmUser queryById(Long userId) {
		return this.userMapper.queryById(userId);
	}

	/**
	 * 分页查询
	 *
	 * @param crmUser     筛选条件
	 * @param pageRequest 分页对象
	 * @return 查询结果
	 */
	@Override
	public Page<CrmUser> queryByPage(CrmUser crmUser, PageRequest pageRequest) {
		long total = this.userMapper.count(crmUser);
		return new PageImpl<>(this.userMapper.queryAllByLimit(crmUser, pageRequest), pageRequest, total);
	}

	/**
	 * 新增数据
	 *
	 * @param crmUser 实例对象
	 * @return 实例对象
	 */
	@Override
	public CrmUser insert(CrmUser crmUser) {
		this.userMapper.insert(crmUser);
		return crmUser;
	}

	/**
	 * 修改数据
	 *
	 * @param crmUser 实例对象
	 * @return 实例对象
	 */
	@Override
	public CrmUser update(CrmUser crmUser) {
		this.userMapper.update(crmUser);
		return this.queryById(crmUser.getUserId());
	}

	/**
	 * 通过主键删除数据
	 *
	 * @param userId 主键
	 * @return 是否成功
	 */
	@Override
	public boolean deleteById(Long userId) {
		return this.userMapper.deleteById(userId) > 0;
	}
}
